package com.vivo.Utils;

import com.thinkgem.jeesite.common.supcan.common.Common;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CommonUtils {

    public static Map<String, String> mapDgAttr = new HashMap<String, String>();
    public static Map<String, String> mapPeopleAttr = new HashMap<String, String>();

    static {
        mapDgAttr.put("V顾问-2", "5");
        mapDgAttr.put("V顾问-3", "4");
        mapDgAttr.put("V顾问-1", "2");
        mapDgAttr.put("V店长-2", "6");
        mapDgAttr.put("V店长-3", "4");
        mapDgAttr.put("V店长-1", "2");
        mapDgAttr.put("导购-2", "1");
        mapDgAttr.put("导购-3", "4");
        mapDgAttr.put("导购-1", "2");
        mapDgAttr.put("共建导购-2", "1");
        mapDgAttr.put("业务代表-2", "3");
        mapDgAttr.put("业务代表-3", "4");
        mapDgAttr.put("业务代表-1", "2");
        mapDgAttr.put("反聘-2", "4");
        mapDgAttr.put("反聘-3", "4");
        mapDgAttr.put("反聘-1", "4");

        mapPeopleAttr.put("1-ah", "5ABBE292AE384E40ACD40E5AD01F71DA");
        mapPeopleAttr.put("2-ah", "C8EC6BE7782A4D2784791B1F3576DCDD");
        mapPeopleAttr.put("3-ah", "EC180C3A50074365841ADA93756C7841");
        mapPeopleAttr.put("1-js", "220BB509254246E985A6FEA4A2060DAD");
        mapPeopleAttr.put("2-js", "928B3F2CFD3F4B36A09FD704682BA06B");
        mapPeopleAttr.put("3-js", "B76B77E89F6D47C29EEC9082B2794290");
    }

    public static int GetAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("生日日期不能是未来日期！");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
        }

        System.out.println("age is :" + age);

        return age;
    }

    public static int getWorkMonth(Date indutyDate){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c2.setTime(indutyDate);

        if (c1.before(indutyDate)) {
            throw new IllegalArgumentException("入职日期不能是未来日期！");
        }

        if(c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if(month1 < month2 || month1 == month2 && day1 < day2) yearInterval --;
        // 获取月数差值
        int monthInterval =  (month1 + 12) - month2  ;
        if(day1 < day2) monthInterval --;
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }

    public static int getWorkYear(Date indutyDate) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c2.setTime(indutyDate);

        int indutyYear = c1.get(Calendar.YEAR);
        int curYear = c2.get(Calendar.YEAR);

        return curYear - indutyYear;
    }

    public static Double getShixiMonths(Date indutyDate, Date ondutyDate) {
        long indutyTime = indutyDate.getTime();
        long ondutyTime = ondutyDate.getTime();

        int days = (int)((ondutyTime - indutyTime) / (1000 * 60 *60 * 24));

        return days / 30.0;
    }


    public static String getNextCode(String code) {
        String newCode = null;

        int length = code.length();

        String codePrefix = code.substring(0, length - 2);
        String codeB = code.substring(length - 2, length);

        int itemp = CommonUtils.hex2int(codeB);
        String codeRet = CommonUtils.int2hex(itemp + 1).substring(6, 8);

        newCode = codePrefix + codeRet;


        return newCode;
    }

    public static String getNewCode(String code) {
        String newCode = code + "01";
        return newCode;
    }



    private static int getHex(int inch)
    {
        if (inch >= 10)
        {
            return inch + 55;
        }
        else
        {
            return inch + 48;
        }
    }

    /**
     * 整型转换成16进制,,负数最后要在最高位加1就是8
     *
     * @param nInt int
     * @return 16进制度字符串。
     */
    public final static String int2hex(int nInt)
    {
        int n = nInt, tmp;
        // 不断的移动，4个分一小段，，移位和与或操作。
        char[] bb = new char[8];
        for (int i = 0; i < 32; i += 4)
        {
            tmp = (int) ((n >> i) & 0x0000000f);
            tmp = getHex(tmp);
            bb[7 - i / 4] = (char) tmp;
        }
        return new String(bb);
    }

    private final static String HEX = "0123456789ABCDEF";

    public final static int hex2int(String hex)
    {
        int ret = 0;
        for (int i = 0, len = hex.length(); i < len; i++)
        {
            ret = (ret << 8) + HEX.indexOf(hex.charAt(i));
        }

        return ret;
    }


    /**
     * 将科学计数法的数字字符转换为文本字符
     * @param number
     * @return
     */
    public final static String getRightNumber(String number) {
        if (number != null && number.contains("E")) {
            DecimalFormat df = new DecimalFormat("#");
            Double tempD = Double.parseDouble(number);
            return df.format(tempD);
        }

        return number;
    }




    public static void main(String args[]) {
//        System.out.println("Hello World!");
//
//        System.out.println(CommonUtils.getNewCode("090A"));
//        System.out.println(CommonUtils.getNewCode("09"));
//        System.out.println(CommonUtils.getNewCode("0901"));
//
//        System.out.println(CommonUtils.getNextCode("090A"));
//        System.out.println(CommonUtils.getNextCode("09"));
//        System.out.println(CommonUtils.getNextCode("0901"));
//
//        System.out.println(CommonUtils.hex2int("09"));
//        System.out.println(CommonUtils.hex2int("0A"));
//        System.out.println(CommonUtils.hex2int("0B"));
//
//
//        System.out.println(CommonUtils.int2hex(12).substring(6, 8));


        CommonUtils.sendSMSMsg("18260412660", "【vivo】测试");
    }


    public static void sendSMSMsg(String phoneNum, String content) {
        content = "【vivo】" + content;
        HashMap paramMap = new HashMap();
        paramMap.put("userCode", "BSDZCF");
        paramMap.put("userPass", "BSDZcf0129");
        paramMap.put("DesNo", phoneNum);
        paramMap.put("Msg", content);
        paramMap.put("Channel", "0");

        HttpUtil.post("http://120.55.197.77:1210/Services/MsgSend.asmx/SendMsg", paramMap);
    }


    public static String getDgAttr(String postName, String postAttr) {
        return mapDgAttr.get(postName + "-" + postAttr);
    }

    /**
     * 获取不同区域的人员属性
     * @param empAttr
     * @param area
     * @return
     */
    public static String getPeopleAttr(String empAttr, String area) {
        return mapPeopleAttr.get(empAttr + "-" + area);
    }
}
